
#include <string.h>
#include <stdio.h>

#include "lang/morestdlib.h"
#include "util/debug.h"
#include "util/error.h"

ErrorPtr _errorNew(const char* filename,int nLine,ErrorPtr root,int errno,const char* message)
    {
    ErrorPtr ptr= (ErrorPtr)Calloc(1,sizeof(Error));
    if(ptr==NULL)
        {
        DEBUG1("Cannot create new error !");
        return root;
        }
     if(filename!=NULL) strncpy(ptr->filename,filename,FILENAME_MAX);
     if(message!=NULL) strncpy(ptr->message,message,ERROR_MAX_MESSAGE_LENGTH);
     ptr->errno=errno;
     ptr->nLine=nLine;
     ptr->next=root;
     return ptr;
    }

void errorDelete(ErrorPtr ptr)
    {
    if(ptr==NULL) return;
    errorDelete(ptr->next);
    Free(ptr);
    }
extern void errorPrint(const ErrorPtr ptr,FILE* out)
    {
    if(ptr==NULL) return;
    fprintf(out,"[ERROR][%s:%d] (%d) : \"%s\"\n",
            ptr->filename,
            ptr->nLine,
            ptr->errno,
            ptr->message
            ) ;
    errorPrint(ptr->next,out);
    fflush(out);
    }